package com.ruoyi.system.utils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.ScrTotal;
import com.ruoyi.system.service.IScrTotalService;
import com.ruoyi.system.service.impl.ScrTotalServiceImpl;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;

public class ExcelEditor {
    String excelFilePath = "";
    FileInputStream inputStream = null;
    Workbook workbook = null;
    Sheet sheet = null;
    public ExcelEditor(String path) throws IOException {
        excelFilePath = path;
        inputStream = new FileInputStream(new File(excelFilePath));
        workbook = new XSSFWorkbook(inputStream);
        sheet = workbook.getSheetAt(0);
    }

    public void setCellValue(int row, int col, String value){
        Row r = sheet.getRow(row);
        if(r == null){
            r = sheet.createRow(row);
        }
        Cell cell = r.getCell(col);
        if(cell == null){
            cell = r.createCell(col);
        }
        cell.setCellValue(value);
    }

    public String getCellValue(int row, int col) {
        Row r = sheet.getRow(row);
        if (r == null) {
            return null;
        }
        Cell cell = r.getCell(col);
        if (cell == null) {
            return null;
        }
        DataFormatter formatter = new DataFormatter();
        return formatter.formatCellValue(cell);
    }

    public void update(ScrTotal scrTotal) throws IOException {
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("MMSE total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getMMSE().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("MOCA total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getMOCA().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("NPI1 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getNPI1().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("NPI2 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getNPI2().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("ADL total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getADL().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("CDR total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getCDR().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("DST1 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getDST1().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("DST2 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getDST2().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("AVLT-N1 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getAvltN1().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("AVLT-N2 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getAvltN2().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("AVLT-N3 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getAvltN3().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("AVLT-N7 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getAvltN7().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("AVLT-N6 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getAvltN6().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("AVLT-N5 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getAvltN5().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("AVLT-N4 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getAvltN4().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("TMT1A time")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getTmt1a().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("TMT1B time")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getTmt1b().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("TMT2A time")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getTmt2a().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("TMT2B time")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getTmt2b().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("HIS total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getHIS().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("TMT1B time")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getTmt1b().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("GDS total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getGDS().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("PSQI total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getPSQI().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("SCD total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getSCDS().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("CFT total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getCFT().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("ADQ2 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getAQD2().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("CDT total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getCDT().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("ADQ1 total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getAQD1().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("HAMD total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getHAMD().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        for(int i = 0;i < 100;i++){
            for(int j = 0;j < 30;j++){
                String res = getCellValue(i, j);
                if(res == null) res = "";
                if(res.equals("HAMA total")){
                    try {
                        setCellValue(i + 1, j, scrTotal.getHAMA().toString());
                    }
                    catch (Exception e){
                        System.out.println(e);
                    }
                }
            }
        }
        finish();
    }

    public void finish() throws IOException {
        inputStream.close();
        FileOutputStream outputStream = new FileOutputStream(excelFilePath);
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();
    }
}
